//
//  JCPlanView.m
//  JiFengDaRen
//
//  Created by shouhan on 2018/3/30.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "JCPlanView.h"
#import "UIButton+ImageTitleSapce.h"

@interface JCPlanView ()

@property (nonatomic, strong) UIView * numberView;

@property (nonatomic, strong) UILabel * numLabel;

@property (nonatomic, strong) UILabel * numName;

@property (nonatomic, strong) UIView * typeView;// 还款类别

@property (nonatomic, strong) UILabel * typeLabel;

@property (nonatomic, strong) UIView * timeView;//  还款时间

@property (nonatomic, strong) UILabel * timeLabel;

@property (nonatomic, strong) UIView * bankView;// 省份和城市

@property (nonatomic, strong) UIView * provinceView;// 省份

@property (nonatomic, strong) UILabel * provinceLabel;


@property (nonatomic, strong) UIView * cityView;// 城市

@property (nonatomic, strong) UILabel * cityLabel;


@property (nonatomic, strong) UILabel * addPlan;// 添加还款笔数

@property (nonatomic, strong) UIImageView * arrow1;
@property (nonatomic, strong) UIImageView * arrow2;
@property (nonatomic, strong) UIImageView * arrow3;
@property (nonatomic, strong) UIImageView * arrow4;

@end



@implementation JCPlanView


- (instancetype)initWithFrame:(CGRect)frame{
 
    self = [super initWithFrame:frame];
    
    if (self) {
        [self initUI];

    }
    return self;
}


- (void)initUI{
    self.backgroundColor = KBASEBGCOLOR;
    
    self.numberView = [UIView new];
    self.numberView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.numberView];
    
    self.numLabel = [UILabel new];
    self.numLabel.text = @"还款卡号";
    self.numLabel.textColor = [UIColor colorWithHexString:@"#474958"];
    self.numLabel.font = KSystemFont(__I6Width(13));
    [self.numberView addSubview:self.numLabel];
    
    self.numName = [UILabel new];
    self.numName.textAlignment = NSTextAlignmentRight;
    self.numName.textColor = [UIColor colorWithHexString:@"#474958"];
    self.numName.font = KSystemFont(__I6Width(13));
    [self.numberView addSubview:self.numName];
    
    self.typeView = [UIView new];
    self.typeView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.typeView];
    
    self.typeLabel = [UILabel new];
    self.typeLabel.text = @"还款类别";
    self.typeLabel.textColor = [UIColor colorWithHexString:@"#474958"];
    self.typeLabel.font = KSystemFont(__I6Width(13));
    [self.typeView addSubview:self.typeLabel];
    
    self.typeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.typeBtn setTitle:@"小额落地商户" forState:UIControlStateNormal];
    [self.typeBtn setTitleColor:[UIColor colorWithHexString:@"#474958"] forState:UIControlStateNormal];
    self.typeBtn.titleLabel.font = KSystemFont(__I6Width(13));
    self.typeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    self.typeBtn.tag = 10001;
    [self.typeBtn addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.typeView addSubview:self.typeBtn];
    
    self.timeView = [UIView new];
    self.timeView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.timeView];
    
    self.timeLabel = [UILabel new];
    self.timeLabel.text = @"还款时间";
    self.timeLabel.textColor = [UIColor colorWithHexString:@"#474958"];
    self.timeLabel.font = KSystemFont(__I6Width(13));
    [self.timeView addSubview:self.timeLabel];
    
    self.timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.timeBtn setTitleColor:[UIColor colorWithHexString:@"#474958"] forState:UIControlStateNormal];
    self.timeBtn.titleLabel.font = KSystemFont(__I6Width(13));
    self.timeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    self.timeBtn.tag = 10002;
    [self.timeBtn addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.timeView addSubview:self.timeBtn];
    
    self.bankView = [UIView new];
    self.bankView.backgroundColor = KBASEBGCOLOR;
    [self addSubview:self.bankView];
    
    self.provinceView = [UIView new];
    self.provinceView.backgroundColor = [UIColor whiteColor];
    [self.bankView addSubview:self.provinceView];
    
    self.provinceLabel = [UILabel new];
    self.provinceLabel.text = @"银行卡所属省份";
    self.provinceLabel.textColor = [UIColor colorWithHexString:@"#474958"];
    self.provinceLabel.font = KSystemFont(__I6Width(13));
    [self.provinceView addSubview:self.provinceLabel];
    
    self.provinceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.provinceBtn setTitleColor:[UIColor colorWithHexString:@"#474958"] forState:UIControlStateNormal];
    self.provinceBtn.titleLabel.font = KSystemFont(__I6Width(13));
    self.provinceBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    self.provinceBtn.tag = 10003;
    [self.provinceBtn addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.bankView addSubview:self.provinceBtn];
    
    self.cityView = [UIView new];
    self.cityView.backgroundColor = [UIColor whiteColor];
    [self.bankView addSubview:self.cityView];
    
    self.cityLabel = [UILabel new];
    self.cityLabel.text = @"银行卡所属城市";
    self.cityLabel.textColor = [UIColor colorWithHexString:@"#474958"];
    self.cityLabel.font = KSystemFont(__I6Width(13));
    [self.cityView addSubview:self.cityLabel];
    
    self.cityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cityBtn setTitleColor:[UIColor colorWithHexString:@"#474958"] forState:UIControlStateNormal];
    self.cityBtn.titleLabel.font = KSystemFont(__I6Width(13));
    self.cityBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    self.cityBtn.tag = 10004;
    [self.cityBtn addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.cityView addSubview:self.cityBtn];
    
    self.addPlan = [UILabel new];
    self.addPlan.text = @"  添加还款笔数";
    self.addPlan.backgroundColor = KBASEBGCOLOR;
    self.addPlan.font = KSHSystemFont(13);
    [self addSubview:self.addPlan];
    
    self.arrow1 = [UIImageView new];
    self.arrow1.image = [UIImage imageNamed:@"jmarrow"];
    [self.typeView addSubview:self.arrow1];
    
    self.arrow2 = [UIImageView new];
    self.arrow2.image = [UIImage imageNamed:@"jmarrow"];
    [self.timeView addSubview:self.arrow2];
    
    self.arrow3 = [UIImageView new];
    self.arrow3.image = [UIImage imageNamed:@"jmarrow"];
    [self.provinceView addSubview:self.arrow3];

    self.arrow4 = [UIImageView new];
    self.arrow4.image = [UIImage imageNamed:@"jmarrow"];
    [self.cityView addSubview:self.arrow4];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    [self.numberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self).offset(__I6Height(10));
        make.height.mas_offset(__I6Height(50));
    }];
    
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.numberView).offset(__I6Width(15));
        make.centerY.equalTo(self.numberView);
        make.width.mas_offset(__I6Width(150));
    }];
    
    [self.numName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.numberView).offset(-__I6Width(15));
        make.centerY.equalTo(self.numberView);
        make.left.equalTo(self.numLabel.mas_right).offset(__I6Width(10));
    }];
    
    [self.typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.numberView.mas_bottom).offset(1);
        make.left.right.equalTo(self);
        make.height.equalTo(self.numberView.mas_height);
    }];

    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeView).offset(__I6Width(15));
        make.centerY.equalTo(self.typeView);
        make.width.mas_offset(__I6Width(150));
    }];

    [self.typeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.typeView);
        make.right.equalTo(self.typeView).offset(-__I6Width(30));
        make.left.equalTo(self.typeLabel.mas_right).offset(__I6Width(10));
    }];

    [self.timeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.typeView.mas_bottom).offset(1);
        make.height.equalTo(self.typeView.mas_height);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeView).offset(__I6Width(15));
        make.centerY.equalTo(self.timeView);
        make.width.mas_offset(__I6Width(150));
    }];
    
    [self.timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.timeView);
        make.right.equalTo(self.timeView).offset(-__I6Width(30));
        make.left.equalTo(self.timeLabel.mas_right).offset(__I6Width(15));
    }];
    
    [self.bankView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.timeView.mas_bottom).offset(1);
        make.height.mas_offset(__I6Height(50) * 2 + 1);
    }];
    
    [self.provinceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.bankView);
        make.height.mas_offset(__I6Height(50));
    }];
    
    [self.provinceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.provinceView).offset(__I6Width(15));
        make.top.bottom.equalTo(self.provinceView);
        make.width.mas_offset(__I6Width(150));
    }];
    
    [self.provinceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.provinceView);
        make.right.equalTo(self.provinceView).offset(-__I6Width(30));
        make.left.equalTo(self.provinceLabel.mas_right).offset(__I6Width(10));
    }];
    
    [self.cityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.bankView);
        make.top.equalTo(self.provinceView.mas_bottom).offset(1);
    }];
    
    [self.cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.cityView);
        make.left.equalTo(self.cityView).offset(__I6Width(15));
        make.width.mas_offset(__I6Width(150));
    }];
    
    [self.cityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.cityView);
        make.right.equalTo(self.cityView).offset(-__I6Width(30));
        make.left.equalTo(self.cityLabel.mas_right).offset(__I6Width(10));
    }];
    
    [self.addPlan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.bankView.mas_bottom);
        make.height.mas_offset(__I6Height(50));
    }];
    
    [self.arrow1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.typeView).offset(-__I6Width(15));
        make.centerY.equalTo(self.typeView);
    }];
    
    [self.arrow2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.timeView).offset(-__I6Width(15));
        make.centerY.equalTo(self.timeView);
    }];
    
    [self.arrow3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.provinceView).offset(-__I6Width(15));
        make.centerY.equalTo(self.provinceView);
    }];
    
    [self.arrow4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.cityView).offset(-__I6Width(15));
        make.centerY.equalTo(self.cityView);
    }];
}

- (void)BtnAction:(UIButton *)btn{
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@(btn.tag),@"tag", nil];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"plan" object:nil userInfo:dic];
}



- (void)updateUI:(NSString * )str{
    if ([str isEqualToString:@"大额还款"]) {
        [self.addPlan mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeView.mas_bottom);
        }];
        
        //更新约束
        [UIView animateWithDuration:0.3 animations:^{
            
            [self layoutIfNeeded];
            
        }completion:^(BOOL finished) {
            self.bankView.hidden = YES;
        }];
        
    }else{
        
        [self.addPlan mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bankView.mas_bottom).offset(0);
            make.left.right.equalTo(self);
            make.height.mas_offset(__I6Height(50));
        }];
        
        //更新约束
        [UIView animateWithDuration:0.3 animations:^{

            [self layoutIfNeeded];

        }completion:^(BOOL finished) {
            self.bankView.hidden = NO;
        }];
    }
}


- (void)getPickerAreaProvince:(NSString *)province City:(NSString *)city{
    
    [self.provinceBtn setTitle:province forState:UIControlStateNormal];
    
    [self.cityBtn setTitle:city forState:UIControlStateNormal];
}




- (void)setModel:(CreditManageModel *)model{
    _model = model;
    self.numName.text = model.cardNo;

}


@end
